package com.stenio.fancat.service.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.shop.*;
import com.stenio.fancat.form.shop.CateogryForm;
import com.stenio.fancat.form.shop.ShopForm;
import com.stenio.fancat.form.shop.VenderForm;
import com.stenio.fancat.mapper.shop.FcCategoryMapper;
import com.stenio.fancat.mapper.shop.FcVenderMapper;
import com.stenio.fancat.service.enums.ImageType;
import com.stenio.fancat.service.enums.SaleStatus;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class VenderService extends ServiceImpl<FcVenderMapper, FcVender> {

	@Autowired
	private FcVenderMapper fcVenderMapper;

	@Autowired
	private VenderImageService venderImageService;

	private static final Logger logger = LoggerFactory.getLogger(VenderService.class);

	public FcVender findById(Integer id){
		return fcVenderMapper.findById(id);
	}

	public List<FcVender> findAll(){
		return fcVenderMapper.findAll();
	}

	/**
	 * 根据条件查询
	 * @param venderForm
	 * @return
	 */
	public Page<FcVender> selectByPage(VenderForm venderForm){
		FcVender fcVender = new FcVender();
		BeanUtils.copyProperties(venderForm,fcVender);
		Page<FcVender> page = new Page<>(venderForm.getPageNo(), venderForm.getPageSize());
		page.setRecords(fcVenderMapper.selectByConditionPage(page,fcVender));
		return page;
	}

	/**
	 * 保存商户
	 * @param form
	 * @return
	 */
	public FcVender saveWare(VenderForm form){
		FcVender vender = new FcVender();
		BeanUtils.copyProperties(form,vender);
		Date date = new Date();
		vender.setCreateTime(date);
		vender.setUpdateTime(date);
		fcVenderMapper.insert(vender);

		//处理关联图片的逻辑
		if(CollectionUtils.isNotEmpty(form.getVenderImages())){
			form.getVenderImages().stream().forEach(vo->{
				venderImageService.updateBiz(vender.getId(),vo);
			});
		}
		return vender;
	}

	/**
	 * 更新商户
	 * @param form
	 */
	public FcVender updateVender(VenderForm form){
		FcVender vender = new FcVender();
		BeanUtils.copyProperties(form,vender);
		fcVenderMapper.updateAll(vender);
		//处理关联图片的逻辑
		FcVenderImage venderImage = new FcVenderImage();
		venderImage.setVenderId(form.getId());
		List<FcVenderImage> relatedImages = new ArrayList<>();
		relatedImages.addAll(venderImageService.selectByType(venderImage));
		//对于已经建立过关联的记录
		List<Integer> relatedIds = new ArrayList<>();
		relatedImages.stream().forEach(vo->{
			//前端记录中不存在，则删除该存在的记录
			if(!form.getVenderImages().contains(vo.getId())){
				venderImageService.deleteById(vo.getId());
			}
			relatedIds.add(vo.getId());
		});
		//对于前端传入的记录
		if(CollectionUtils.isNotEmpty(form.getVenderImages())){
			form.getVenderImages().stream().forEach(vo->{
				//若已关联的记录中，不存在该记录，则建立关联，否则保持不动
				if(!relatedIds.contains(vo)){
					venderImageService.updateBiz(vender.getId(),vo);
				}
			});
		}
		return vender;
	}

}
